﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace SocketChat.Core.Sockets
{
    [Serializable]
    public class Msg
    {
        public MsgType Header { get; set; }

        /// <summary>
        /// 发送消息的Socket的Id
        /// </summary>
        public Guid Id { get; set; }

        public string Body { get; set; }

        public override string ToString() {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(this);
        }

        public static Msg Prase(string input) {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Deserialize<Msg>(input);
        }

        public static bool TryPrase(string input, out Msg message) {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try {
                message = jss.Deserialize<Msg>(input);
                return true;
            }
            catch {
                message = null;
                return false;
            }
        }
    }
}
